//od机试简单_身高差排序
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

#define PF(...) printf(__VA_ARGS__)

struct Height {
    int absHeight;
    int nHeight;
};

int main_1() {
    int H = 0;  // xiaoming
    int N = 0;  // 人数
    while (cin >> H >> N) {
        vector<Height> vtHeight(N);
        for (size_t i = 0; i < N; i++) {
            int tmp;
            cin >> tmp;
            vtHeight[i].nHeight = tmp;
            vtHeight[i].absHeight = std::abs(tmp - H);
        }
        // 测试
        //for (auto [ab, h] : vtHeight) PF("%d,%d\n", ab, h); PF("\n");

        std::sort(vtHeight.begin(), vtHeight.end(), [](Height h1, Height h2){
            if (h1.absHeight == h2.absHeight) {
                return h1.nHeight < h2.nHeight;
            }
            return h1.absHeight < h2.absHeight;
        });
        for (auto[absH, hei] : vtHeight) {
            printf("%d ", hei);
        }
        printf("\n");
    }
    return 0;
}
